package com.intellagric.service.impl.module.cms_content;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.CmsCategoryMapper;
import com.intellagric.pojo.CmsCategory;
import com.intellagric.pojo.CmsCategoryExample;
import com.intellagric.service.module.cms_content.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2018/10/19
 * @Description: 前台网站内容分类管理
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private CmsCategoryMapper contentCategoryMapper;

    /**
     * 添加内容分类信息
     * @param contentCategory
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addContentCategory(CmsCategory contentCategory) {
        if (contentCategoryMapper.insertSelective(contentCategory) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除内容分类信息
     * @param id
     * @return int
     */
    @Override
    public ResponseMessage deleteContentCategory(String id) {
        if (contentCategoryMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改内容分类信息
     * @param contentCategory
     * @return int
     */
    @Override
    public ResponseMessage editContentCategory(CmsCategory contentCategory) {
        if (contentCategoryMapper.updateByPrimaryKeySelective(contentCategory) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询单个内容分类信息
     * @param id
     * @return ContentCategory
     */
    @Override
    public CmsCategory queryContentCategoryById(String id) {
        return contentCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有内容分类信息
     * @return List<ContentCategory>
     */
    @Override
    public LayuiDataGridResult queryContentCategoryList() {
        CmsCategoryExample contentCategoryExample = new CmsCategoryExample();
        contentCategoryExample.setOrderByClause("sort");
        contentCategoryExample.createCriteria().andIdIsNotNull();
        List<CmsCategory> contentCategoryList = contentCategoryMapper.selectByExample(contentCategoryExample);
        return LayuiDataGridResult.success().add(contentCategoryList,contentCategoryList.size());
    }

    /**
     * 异步查询内容分类信息，通过父id查询
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult queryContentCategoryByPid(String parentId) {
        CmsCategoryExample contentCategoryExample = new CmsCategoryExample();
        contentCategoryExample.setOrderByClause("sort");
        contentCategoryExample.createCriteria().andParentIdEqualTo(parentId);
        List<CmsCategory> cmsCategoryList = contentCategoryMapper.selectByExample(contentCategoryExample);
        return LayuiDataGridResult.success().add(cmsCategoryList,cmsCategoryList.size());
    }

    /**
     * 查询内容分类数据信息，生成树形菜单
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getContentCategoryTree() {
        //查询所有列表数据
        List<CmsCategory> contentCategoryList = this.queryContentCategoryList().getData();

        //查询顶级菜单
        List<CmsCategory> rootCategoryList = this.queryContentCategoryByPid("0").getData();

        //生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
        for (CmsCategory contentCategory : rootCategoryList) {
            contentCategory.setChildren(getChild(contentCategory.getId(), contentCategoryList));
        }
        return LayuiDataGridResult.success().add(rootCategoryList,rootCategoryList.size());

    }

    private List<CmsCategory> getChild(String id,List<CmsCategory> contentCategoryList) {
        // 子菜单
        List<CmsCategory> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (CmsCategory contentCategory : contentCategoryList) {
            if (contentCategory.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (contentCategory.getParentId().equals(id)) {
                    childList.add(contentCategory);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (CmsCategory contentCategory : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            contentCategory.setChildren(getChild(contentCategory.getId(), contentCategoryList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }




    
}
